﻿CREATE VIEW [dbo].[TempArticleSalesCategoryMargins]
	AS
		select 
		c.Id, 
		c.Name, 
		Case when SUM(s.Sales) > 0 then SUM(s.GM) / SUM(s.Sales) end as Margin
		from TempArticleSales s
		inner join Articles a on a.Id = s.ArticleId
		inner join Categories c on c.Id = a.CategoryId
		group by c.Id, c.Name